package org.jaaksi.demo.ptr.ui;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import org.jaaksi.demo.ptr.R;
import org.jaaksi.demo.ptr.api.MockApi;
import org.jaaksi.demo.ptr.api.NetApi;
import org.jaaksi.demo.ptr.bean.response.Result;
import org.jaaksi.demo.ptr.github.ConfigInfoVo;
import org.jaaksi.demo.ptr.util.DataParseUtil;
import retrofit.call.SimpleCallback;
import retrofit.util.ServiceGenerator;
import retrofit2.Call;
import retrofit2.http.GET;

/**
 * 测试自定义gson解析
 */
public class TestActivity extends FragmentActivity {

  private TextView mTextView;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_github);

    mTextView = (TextView) findViewById(R.id.tv_response);

    Call<Result<ConfigInfoVo>> call = ServiceGenerator.createGithubService(NetApi.class).getData();
    call.enqueue(new SimpleCallback<Result<ConfigInfoVo>>() {
      @Override public void networkError(Call<Result<ConfigInfoVo>> call, IOException ex) {
        mTextView.setText("无网络");
      }

      @Override public void onResponse(Call<Result<ConfigInfoVo>> call, Result<ConfigInfoVo> entity,
        boolean isSuccess) {
        if (entity != null) {
          String json = DataParseUtil.mGson.toJson(entity);
          mTextView.setText(json);
        }
      }
    });
  }

  void test() {
    // retrofit使用动态代理方式
    MockApi iTest = (MockApi) Proxy.newProxyInstance(MockApi.class.getClassLoader(),
      new Class<?>[] { MockApi.class }, new InvocationHandler() {
        @Override public Object invoke(Object proxy, Method method, Object[] args)
          throws Throwable {
          System.out.println("args.length = " + args.length);
          Integer a = (Integer) args[0];
          Integer b = (Integer) args[1];
          System.out.println("方法名：" + method.getName());
          System.out.println("参数：" + a + " , " + b);

          GET get = method.getAnnotation(GET.class);
          System.out.println("注解：" + get.value());
          return null;
        }
      });
    // 当接口调用MockApi的任意方法的时候都会回调 invoke 方法
    iTest.add(3, 5);
  }
}
